﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using Microsoft.SharePoint;
using System.Data;
using JM.JCY.Common;

namespace JM.JCY.SharePointComponent.DownloadCenter.ViewAllDownload
{
    public partial class ViewAllDownloadUserControl : UserControl
    {
        string listName = JCYConstant.SharePointListName.DownloadCenter;
        protected void Page_Load(object sender, EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            SPList downloadList = web.Lists[listName];
            SPQuery query = new SPQuery();
            query.Query = "<OrderBy><FieldRef Name='Modified' Ascending='FALSE' /></OrderBy>";

            SPListItemCollection itemCols = downloadList.GetItems(query);

            DataTable dt = new DataTable();
            dt.Columns.Add("Title");
            dt.Columns.Add("Link");

            foreach (SPItem item in itemCols)
            {
                DataRow newRow = dt.NewRow();
                string[] s = item["LinkFilename"].ToString().Split('.');
                newRow["Title"] = s[0].ToString();
                newRow["Link"] = item["FileRef"].ToString();
                dt.Rows.Add(newRow);
            }

            this.gvDownLoadCenter.DataSource = dt;
            this.gvDownLoadCenter.DataBind();


            string onclick = string.Format("javascript:NewItem2(event, '{0}');javascript:return false;", downloadList.DefaultNewFormUrl);
            this.idHomePageNewItem.Attributes.Add("onclick", onclick);

            if (CommonFunction.IsApplicationAdmin(12))
                this.idHomePageNewItem.Visible = true;
            else
                this.idHomePageNewItem.Visible = false;

        }


        protected void gvDownLoadCenter_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Download")
            {
                string fileName = e.CommandArgument.ToString().Substring(e.CommandArgument.ToString().LastIndexOf("/") + 1);
                OpenAttachment(e.CommandArgument.ToString(), fileName);
            }
        }

        public void OpenAttachment(string url, string fileName)
        {
            try
            {
                SPWeb web = SPContext.Current.Web;
                web.AllowUnsafeUpdates = true;

                SPFile tempFile = web.GetFile(url);

                byte[] obj = (byte[])tempFile.OpenBinary();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename= " + fileName);
                HttpContext.Current.Response.ContentType = "application/octet-stream";

                //Check that the client is connected and has not closed the connection after the request

                if (HttpContext.Current.Response.IsClientConnected)
                    HttpContext.Current.Response.BinaryWrite(obj);

                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.Close();

                web.AllowUnsafeUpdates = false;
            }

            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }



    }
}
